﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.DataServices.DataSources;
using DiscoveryLogic.UI.Layouts.Converters;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class HelocCylinderAxis : UserControl, IGraphControl
    {
        #region Private Variables

            private double delta;
            private Account model;
            private double homeTaxValue = 82092;
            private double maxAmount;
            private double minAmount = 0;
            private double currentAmount = 0;

        #endregion

        #region Constructors

            public HelocCylinderAxis()
            {
                InitializeComponent();

                maxAmount = 0.8 * homeTaxValue;
            }

        #endregion

        #region Get/Set Properties

            public Account Model
            {
                get { return model; }
                set{
                    model = value;
                    DrawAmountMarks((MortgageAccount)model);
                }
            }

        #endregion

        #region Mouse Events

            // Used to help track slider dragging
            private double mouseDownPosition = 0;
            private double mouseDownValue = -1;

            protected void SliderThumb_MouseEnter(object sender, EventArgs e)
            {
                Ellipse sliderThumb = (Ellipse)sender;
                sliderThumb.Cursor = Cursors.Hand;
            }

            protected void SliderThumb_MouseLeave(object sender, EventArgs e)
            {
                Ellipse sliderThumb = (Ellipse)sender;
                sliderThumb.Cursor = Cursors.Arrow;
            }

            protected void SliderThumb_MouseDown(object sender, MouseEventArgs e)
            {
                SliderThumb.CaptureMouse();
                mouseDownValue = (double)SliderThumb.GetValue(Canvas.TopProperty);
                mouseDownPosition = e.GetPosition(null).Y;
            }

            protected void SliderThumb_MouseUp(object sender, EventArgs e)
            {
                SliderThumb.ReleaseMouseCapture();
                mouseDownValue = -1;
            }

            protected void SliderThumb_MouseMove(object sender, MouseEventArgs e)
            {
                // If mouse button is still in down position
                if (mouseDownValue != -1)
                {                    
                    delta = (e.GetPosition(null).Y - mouseDownPosition);

                    // Set slider thumb position
                    SetSlider(delta);               
                }
            }

        #endregion

        #region Helper Methods
            
            private void DrawAmountMarks(MortgageAccount account)
            {
                double sliderThumbHeight = (double)SliderThumb.GetValue(Ellipse.HeightProperty);

                // Find slider bar and its dimensions                
                double sliderBarMax = (double)SliderBar.GetValue(Canvas.TopProperty)
                                    - sliderThumbHeight / 2;
                double sliderBarMin = (double)SliderBar.GetValue(Canvas.TopProperty)
                                    + (double)SliderBar.GetValue(Path.HeightProperty)
                                    - sliderThumbHeight / 2;

                for(int i = 0; i < 5; i++)
                {
                    // Calculate and update notch positions
                    TextBlock notch = Root.FindName("SliderNotch" + i) as TextBlock;

                    double pathLength = sliderBarMin - sliderBarMax;
                    double notchAmount = maxAmount - ((maxAmount / 6) * i);
                    currentAmount = Math.Round(notchAmount);

                    double notchPosition = pathLength * (1 - (notchAmount / maxAmount)) + sliderBarMax;
                                        
                    notch.Text = Math.Round(notchAmount).ToCurrency();
                    notch.SetValue(Canvas.TopProperty, notchPosition);
                }

                SetSlider(maxAmount);
            }

        #endregion

        #region IGraphControl Members

            public event SliderChangedEventHandler SliderChanged;

            public void SetSlider(double deltaY)
            {
                // Find slider thumb and its dimensions
                double sliderThumbHeight = (double)SliderThumb.GetValue(Ellipse.HeightProperty);

                // Find slider bar and its dimensions
                double sliderBarMax = (double)SliderBar.GetValue(Canvas.TopProperty)
                                    - sliderThumbHeight / 2;
                double sliderBarMin = (double)SliderBar.GetValue(Canvas.TopProperty)
                                    + (double)SliderBar.GetValue(Path.HeightProperty)
                                    - sliderThumbHeight / 2;

                double newValue = mouseDownValue + deltaY;

                // Ensure that slider thumb doesn`t go beyond min or max
                if (newValue < sliderBarMax)
                {
                    newValue = sliderBarMax;
                }

                if (newValue > sliderBarMin)
                {
                    newValue = sliderBarMin;
                }

                SliderThumb.SetValue(Canvas.TopProperty, newValue);

                // Calculate and update amount textblock beside slider thumb
                TextBlock sliderAmountText = SliderThumbAmount;
                double pathLength = sliderBarMin - sliderBarMax;
                double sliderPosition = newValue  - sliderBarMax;
                double sliderAmount = maxAmount * (1 - (sliderPosition / pathLength));
                currentAmount = Math.Round(sliderAmount);

                sliderAmountText.Text = Math.Round(sliderAmount).ToCurrency();
                sliderAmountText.SetValue(Canvas.TopProperty, newValue);

                for (int i = 0; i < 5; i++)
                {
                    TextBlock notch = Root.FindName("SliderNotch" + i) as TextBlock;
                    double notchTop = (double)notch.GetValue(Canvas.TopProperty);

                    if (Math.Abs(newValue - notchTop) < sliderAmountText.Height
                        || Math.Abs(notchTop - newValue) < notch.Height)
                    {
                        notch.Opacity = 0;
                    }
                    else{
                        notch.Opacity = 1;
                    }
                }

                // Raise event
                OnSliderChanged(new EventArgs());   

            }

            public double GetLastSliderChange()
            {
                return currentAmount;
            }
                       
            public void OnSliderChanged(EventArgs e)
            {
                if (SliderChanged != null){
                    SliderChanged(this, e);
                }
            }

            public double GetCurrentPosition()
            {
                throw new NotImplementedException();
            }

            public double GetSliderLength()
            {
                throw new NotImplementedException();
            }

            public void SetSliderPercent(double percent)
            {
                throw new NotImplementedException();
            }

        #endregion

        #region Public Methods

            public void ResetSlider()
            {
                SetSlider(maxAmount);
            }

        #endregion

    }
}
